#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import sys

from common.cleaner import clear
from common.common import exter_attack
from common.util.check_utils import is_valid_id
from redis import log

from redis.common.const import ProgressType, SysStdData
from redis.service.cluster_res_service import ClusterResManager
from redis.service.redis_restore_service import RedisRestore
from redis.service.job_progress import JobProgress
from redis.service.redis_job_service import RedisJobManager
from redis.service.redis_backup_service import BackupTaskManager


class RedisObjectApi:
    @staticmethod
    @exter_attack
    def check_application(pid, *args):
        ClusterResManager.check_application(pid)

    @staticmethod
    @exter_attack
    def query_cluster(pid, *args):
        ClusterResManager.query_cluster(pid)

    @staticmethod
    @exter_attack
    def list_application_v2(pid, *args):
        ClusterResManager.list_application_v2(pid)

    @staticmethod
    @exter_attack
    def check_backup_job_type(pid, job_id, *args):
        BackupTaskManager.check_backup_job_type(pid, job_id)

    @staticmethod
    @exter_attack
    def query_job_permission(pid, job_id, *args):
        BackupTaskManager.query_job_permission(pid, job_id)

    @staticmethod
    @exter_attack
    def allow_backup_in_local_node(pid, job_id, sub_job_id):
        BackupTaskManager.allow_backup_in_local_node(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def allow_backup_sub_job_in_local_node(pid, job_id, sub_job_id):
        BackupTaskManager.allow_backup_sub_job_in_local_node(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def backup_pre_job(pid, job_id, *args):
        BackupTaskManager.backup_pre_job(pid, job_id)

    @staticmethod
    @exter_attack
    def backup_common_progress(pid, job_id, sub_job_id):
        BackupTaskManager.backup_common_progress(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def generate_sub_job(pid, job_id, *args):
        BackupTaskManager.generate_sub_job(pid, job_id)

    @staticmethod
    @exter_attack
    def backup(pid, job_id, sub_job_id):
        BackupTaskManager(pid, job_id, sub_job_id).backup()

    @staticmethod
    @exter_attack
    def backup_process(pid, job_id, sub_job_id):
        BackupTaskManager.backup_process(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def backup_post_job(pid, job_id, *args):
        BackupTaskManager.backup_post_job(pid, job_id)

    @staticmethod
    @exter_attack
    def query_backup_copy(pid, job_id, sub_job_id):
        BackupTaskManager.query_backup_copy(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def abort_job(pid, job_id, sub_job_id):
        BackupTaskManager.abort_job(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def pause_job(pid, job_id, sub_job_id):
        RedisJobManager.pause_job(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def allow_restore_in_local_node(pid, job_id, sub_job_id):
        RedisRestore.allow_restore_in_local_node(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def allow_restore_sub_job_in_local_node(pid, job_id, sub_job_id):
        RedisRestore.allow_restore_sub_job_in_local_node(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_prerequisite(pid, job_id, sub_job_id):
        RedisRestore.restore_prerequisite(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_prerequisite_progress(pid, job_id, sub_job_id):
        JobProgress.get_progress(pid, job_id, sub_job_id, ProgressType.RESTORE_PREREQUISITE.value)

    @staticmethod
    @exter_attack
    def restore(pid, job_id, sub_job_id):
        RedisRestore.restore(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_progress(pid, job_id, sub_job_id):
        JobProgress.get_progress(pid, job_id, sub_job_id, ProgressType.RESTORE.value)

    @staticmethod
    @exter_attack
    def restore_post(pid, job_id, sub_job_id):
        RedisRestore.restore_post(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_post_progress(pid, job_id, sub_job_id):
        JobProgress.get_progress(pid, job_id, sub_job_id, ProgressType.RESTORE_POST.value)

    @staticmethod
    @exter_attack
    def restore_gen_sub_job(pid, job_id, sub_job_id):
        RedisRestore.restore_gen_sub_job(pid, job_id, sub_job_id)


func_dict = {
    'CheckApplication': RedisObjectApi.check_application,
    'QueryCluster': RedisObjectApi.query_cluster,
    'ListApplicationResourceV2': RedisObjectApi.list_application_v2,
    'CheckBackupJobType': RedisObjectApi.check_backup_job_type,
    'QueryJobPermission': RedisObjectApi.query_job_permission,
    'AllowBackupInLocalNode': RedisObjectApi.allow_backup_in_local_node,
    'AllowBackupSubJobInLocalNode': RedisObjectApi.allow_backup_sub_job_in_local_node,
    'BackupPrerequisite': RedisObjectApi.backup_pre_job,
    'BackupCommonProgress': RedisObjectApi.backup_common_progress,
    'BackupGenSubJob': RedisObjectApi.generate_sub_job,
    'Backup': RedisObjectApi.backup,
    'BackupProgress': RedisObjectApi.backup_process,
    'BackupPostJob': RedisObjectApi.backup_post_job,
    'QueryBackupCopy': RedisObjectApi.query_backup_copy,
    'AbortJob': RedisObjectApi.abort_job,
    'PauseJob': RedisObjectApi.pause_job,
    'AllowRestoreInLocalNode': RedisObjectApi.allow_restore_in_local_node,
    'AllowRestoreSubJobInLocalNode': RedisObjectApi.allow_restore_sub_job_in_local_node,
    'RestorePrerequisite': RedisObjectApi.restore_prerequisite,
    'RestorePrerequisiteProgress': RedisObjectApi.restore_prerequisite_progress,
    'Restore': RedisObjectApi.restore,
    'RestoreProgress': RedisObjectApi.restore_progress,
    'RestorePostJob': RedisObjectApi.restore_post,
    'RestorePostJobProgress': RedisObjectApi.restore_post_progress,
    'RestoreGenSubJob': RedisObjectApi.restore_gen_sub_job
}

if __name__ == '__main__':
    """
    功能描述：主任务执行
    参数：
    @fun_type：方法类型
    @pid： 请求ID
    @job_id： 主任务任务ID
    @sub_job_id: 子任务ID
    样例：python3 redis_object_api.py Restore 1657539915784 2e0c6c0b faa3e2c0
    """
    log.info('running redis main')
    for line in sys.stdin:
        SysStdData.SYS_STDIN = line
        break
    if SysStdData.SYS_STDIN == '':
        log.error('failed to obtain sys data')
    g_fun_type = sys.argv[1]
    g_pid = sys.argv[2]
    if not is_valid_id(g_pid):
        log.warn(f'g_pid is invalid')
        clear(sys.stdin)
        sys.exit(1)
    g_job_id = ''
    g_sub_job = ''
    log.info(f'main argv len: {len(sys.argv)}')
    if len(sys.argv) >= 4:
        g_job_id = sys.argv[3]
        if not is_valid_id(g_job_id):
            log.warn(f'g_job_id is invalid')
            clear(sys.stdin)
            sys.exit(1)
    if len(sys.argv) >= 5:
        g_sub_job = sys.argv[4]
        if not is_valid_id(g_sub_job):
            log.warn(f'g_sub_job is invalid')
            clear(sys.stdin)
            sys.exit(1)
    try:
        func_dict[g_fun_type](g_pid, g_job_id, g_sub_job)
    except KeyError:
        log.error(f'{g_fun_type} not exist')
    SysStdData.SYS_STDIN = ''
